package com.roamer.school.web.handler;

import com.roamer.school.common.exception.BusinessException;
import com.roamer.school.web.model.ResultBean;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

/**
 * 控制层 统一异常处理器
 *
 * @author roamer
 * @version V1.0
 * @date 2019/1/21 16:35
 */
@RestController
@ControllerAdvice
public class ControllerExceptionHandler extends BaseExceptionHandler {

    /**
     * 处理400类异常
     *
     * @param ex
     * @param request
     *
     * @return
     */
    @Override
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public ResultBean<?> handleConstraintViolationException(
            ConstraintViolationException ex,
            HttpServletRequest request) {
        return super.handleConstraintViolationException(ex, request);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @Override
    public ResultBean<?> handleConstraintViolationException(
            HttpMessageNotReadableException ex,
            HttpServletRequest request) {
        return super.handleConstraintViolationException(ex, request);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    @Override
    public ResultBean<?> handleBindException(BindException ex, HttpServletRequest request) {
        return super.handleBindException(ex, request);
    }

    /**
     * 处理使用Validated注解时，参数验证错误异常
     *
     * @param ex
     * @param request
     *
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @Override
    public ResultBean<?> handleMethodArgumentNotValidException(
            MethodArgumentNotValidException ex,
            HttpServletRequest request) {
        return super.handleMethodArgumentNotValidException(ex, request);
    }

    /**
     * 处理自定义异常
     *
     * @param ex
     * @param request
     *
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @Override
    public ResponseEntity<ResultBean<?>> handleBusinessException(BusinessException ex, HttpServletRequest request) {
        return super.handleBusinessException(ex, request);
    }

    @ExceptionHandler(UnauthorizedException.class)
    @Override
    public ResponseEntity<ResultBean<?>> handleUnauthorizedException(
            UnauthorizedException ex, HttpServletRequest request) {
        return super.handleUnauthorizedException(ex, request);
    }



    /**
     * 处理运行时异常
     *
     * @param ex
     * @param request
     *
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(RuntimeException.class)
    @Override
    public ResultBean<?> handleRuntimeException(RuntimeException ex, HttpServletRequest request) {
        return super.handleRuntimeException(ex, request);
    }
}
